//
//  MKCell.swift
//  Weibo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 MK. All rights reserved.
//

//原创微博的cell = 顶部视图(头＋正文＋配图视图) + 底部视图
//转发微博的cell = 顶部视图(头) ＋ 转发视图(正文＋配图视图) ＋ 底部视图

import UIKit
import SnapKit

class MKCell: UITableViewCell {
    
    //1.创建一个约束属性对象 2.给约束对象赋值 3.干掉约束 4.重置约束
    //FIXME: - 这个要考虑cell重用的问题 所有要取消的约束在更新后还是需要记录此约束
    var constraintB: Constraint?
    
    var statusModel: MKStatusModel? {
        didSet {
            topView.staModel = statusModel
            //retweeteView.retweetStatus = statusModel?.retweeted_status
            //print(statusModel?.retweeted_status)
            
            constraintB!.uninstall()
            
            //根据retweeted_status是否有值 来判断是原创微博还是转发微博
            if let isRetweete = statusModel?.retweeted_status {
                //是转发微博
                retweeteView.retweetStatus = isRetweete
                retweeteView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraintB = make.top.equalTo(retweeteView.snp_bottom).constraint
                })
            } else {
                //是原创微博
                retweeteView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constraintB = make.top.equalTo(topView.snp_bottom).constraint
                })
            }
            
        }
    }
    
    //入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //设置cell选中的样式
        selectionStyle = UITableViewCellSelectionStyle.None
        
        setUpViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置子视图
    func setUpViews() {
        contentView.addSubview(topView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweeteView)
        
        //topView.backgroundColor = UIColor(white: 0.98, alpha: 1.0)
        bottomView.backgroundColor = UIColor.whiteColor()
        retweeteView.backgroundColor = UIColor(white: 0.92, alpha: 1.0)
        
        topView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(contentView)
        }
        
        retweeteView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom).offset(cellIconMargin)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            //2.记录约束
            self.constraintB = make.top.equalTo(retweeteView.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(38)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            //FIXME: - 1.contentView底部距离View底部 2.topView底部距离最底下的子视图的View
            make.bottom.equalTo(bottomView.snp_bottom)
        }
    }
    
    
    //加载子视图
    private lazy var topView: MKCellTopView = MKCellTopView()
    private lazy var bottomView: MKBottomView = MKBottomView()
    //加载子视图-转发视图
    private lazy var retweeteView: MKRetweetedView = MKRetweetedView()
    
}
